package cn.tedu.service;


import cn.tedu.pojo.Car;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestJDBC {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3307/py-school-db?characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        Connection conn = DriverManager.getConnection(url,"root","123456");
        String sql = "select * from car ";
        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        List<Car> list =  new ArrayList<>();
        while (rs.next()){
            Car c = new Car();
            String name = rs.getString(1);
            String color = rs.getString(2);
            double price = rs.getDouble(3);

            c.setName(name);
            c.setColor(color);
            c.setPrice(price);

            list.add(c);
        }
        for (Car car : list) {
            System.out.println(car);
        }

        rs.close();
        ps.close();
        conn.close();
    }


    }


